//
//  SN_WebViewController.m
//  Shiyun_Newspaper
//
//  Created by Sylar on 2017/6/22.
//  Copyright © 2017年 ShiYun. All rights reserved.
//

#import "SN_WebViewController.h"
#import "NJKWebViewProgressView.h"
#import "NJKWebViewProgress.h"

@interface SN_WebViewController ()<UIWebViewDelegate,NJKWebViewProgressDelegate>
{
    NJKWebViewProgressView *_webViewProgressView;
    NJKWebViewProgress *_webViewProgress;
    
    NSInteger loadNum;
    NSInteger  Num;
    UIButton *closeBtn;
    
    UIBarButtonItem *backItem; //左侧导航条
    
    UILabel *titlelabel;
    
    BOOL goBack;
}

@property (nonatomic,strong) UIWebView *webView;

@end

@implementation SN_WebViewController
- (void)loadView {
    
    [super loadView];
    
    [self.view setBackgroundColor:[UIColor whiteColor]];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    
    
    [self.view addSubview:self.webView];
    
    loadNum = 0;
    Num = 0;
    
    _webViewProgress = [[NJKWebViewProgress alloc] init];
    _webView.delegate = _webViewProgress;
    _webViewProgress.webViewProxyDelegate = self;
    _webViewProgress.progressDelegate = self;
    
    
    CGRect navBounds = self.navigationController.navigationBar.bounds;
    CGRect barFrame = CGRectMake(0,
                                 SafeAreaTopHeight - 2,
                                 navBounds.size.width,
                                 2);
    _webViewProgressView = [[NJKWebViewProgressView alloc] initWithFrame:barFrame];
    _webViewProgressView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
    [_webViewProgressView setProgress:0 animated:YES];
    
    _webViewProgressView.tintColor = [UIColor colorWithHexString:@"#1ebfff"];
    _webViewProgressView.progressBarView.tintColor = [UIColor colorWithHexString:@"#1ebfff"];
    //    _webViewProgressView.backgroundColor = [UIColor colorWithHexString:@"#1ebfff"];
    [self.navBar addSubview:_webViewProgressView];
    
    backItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageWithOriginalName:@"web_back"] style:UIBarButtonItemStyleDone target:self action:@selector(back:)];
    
    [self.navBar.navigationItem setLeftBarButtonItem:backItem];
    
    UIImageView *navBackView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 160, 44)];
    navBackView.userInteractionEnabled=YES;
    
    titlelabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 0,160, 44)];
    titlelabel.textAlignment=NSTextAlignmentCenter;
    titlelabel.font = [UIFont boldSystemFontOfSize:18];
    titlelabel.textColor = [UIColor whiteColor];
    
    [navBackView addSubview:titlelabel];
    
    self.navBar.navigationItem.titleView = navBackView;
    
    closeBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [closeBtn setFrame:CGRectMake(65, SafeAreaTopHeight-40 - 2, 50, 40)];
    [closeBtn setTitle:@"关闭" forState:(UIControlStateNormal)];
    closeBtn.titleLabel.font = [UIFont boldSystemFontOfSize:16];
    [closeBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    [closeBtn addTarget:self action:@selector(closeAction) forControlEvents:(UIControlEventTouchUpInside)];
    //    closeBtn.hidden = YES;
    [self.navBar addSubview:closeBtn];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillDisappear:(BOOL)animated {
    
    [super viewWillDisappear:animated];
    
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    [closeBtn removeFromSuperview];
    [_webViewProgressView removeFromSuperview];
}

#pragma mark -返回上一层控制器
-(void)back:(id)sender{
    
    if ([self.webView canGoBack]) {
        
        loadNum =  loadNum -2;
        [self.webView goBack];
        
        goBack = YES;
        
    }else{
        
        [closeBtn removeFromSuperview];
        [_webViewProgressView removeFromSuperview];
        [self.navigationController popViewControllerAnimated:YES];
    }
}


- (void)closeAction {
    
    [_webViewProgressView removeFromSuperview];
    [closeBtn removeFromSuperview];
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark -webviewDelegate

-(void)webViewProgress:(NJKWebViewProgress *)webViewProgress updateProgress:(float)progress
{
    [_webViewProgressView setProgress:progress animated:YES];
}

- (void)webViewDidStartLoad:(UIWebView *)webView{
    
    loadNum += 1;
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    
    NSLog(@"%@",error);
}

-(void)webViewDidFinishLoad:(UIWebView *)webView{
    
    titlelabel.text = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
}

#pragma mark -Getter
- (UIWebView *)webView{
    
    if (_webView == nil) {
        _webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, SafeAreaTopHeight, SCREENWIDTH, SCREENHEIGH - SafeAreaTopHeight)];
        [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:_url]]];
        _webView.delegate = self;
    }
    
    return _webView;
}

@end
